<%@page import="qingke.mybatis.pojo.Store"%>
<%@page import="java.util.HashMap"%>
<%@page import="qingke.mybatis.pojo.Product"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="qingke.bean.CartOfProduct"%>
<%@page import="qingke.mybatis.pojo.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="${pageContext.request.contextPath }/css/personalcenter.css"
	type="text/css" rel="stylesheet" />
<link href="${pageContext.request.contextPath }/css/bootstrap.min.css"
	type="text/css" rel="stylesheet" />
<script src="${pageContext.request.contextPath }/js/jquery-1.8.3.min.js"></script>
<script src="${pageContext.request.contextPath }/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath }/js/personal.js"></script>

</head>
<%
	Object obj = session.getAttribute("user");
	User user = null;
	if (obj != null) {
		if (obj instanceof User) {
			user = (User) obj;
		}
	}
%>
<%
	int number = 0;
	Map<Store, CartOfProduct> cartsMap = new HashMap<>();
	List<CartOfProduct> carts = (List<CartOfProduct>) session.getAttribute("carts");
	if (user != null) {
		if (carts != null) {
			for (CartOfProduct cart : carts) {
				if (cart.getUser().getId().equals(user.getId())) {
					cartsMap.put(cart.getStore(), cart);
				}
			}
		}
	}
%>
<%
	Map<Integer, Product> cartproducts = null;
	Object cartproductsObject = session.getAttribute("cartproducts");
	if (cartproductsObject != null) {
		if (cartproductsObject instanceof Map) {
			cartproducts = (Map<Integer, Product>) cartproductsObject;
		}
	}
%>
<body onload="getProduct()">
	<div class="person-content-location">
		<a href="" role="button">当前位置</a> > <a href="" role="button">个人中心</a>
		> <a href="" role="button">我的购物车</a>
	</div>
	<hr />
	<div>
		<%
			for (Map.Entry<Store, CartOfProduct> entrys : cartsMap.entrySet()) {
				Store store = (Store) entrys.getKey();
				CartOfProduct cartOne = entrys.getValue();
		%>
		<div style="border: 1px dashed blue;">
			<table class="table table-hover">
				<h4><%=store.getStoreName()%></h4>
				<%
					if (cartOne != null && cartproducts != null) {
							int amount = 0;
							double totals = 0.00;
				%>
				<thead>
					<tr>
						<th>编码</th>
						<th>名字</th>
						<th>价格</th>
						<th>数量</th>
						<th>总价</th>
						<th></th>
					</tr>
				</thead>
				<tbody>
					<%
						Iterator iter = cartOne.getProductmap().entrySet().iterator();
								while (iter.hasNext()) {
									Map.Entry entry = (Map.Entry) iter.next();
									Product product = cartproducts.get((Integer) entry.getKey());
									double total = product.getProductPrice() * (Integer) entry.getValue();
									totals += total;
					%>
					<tr>
						<td><%=product.getProductCode()%></td>
						<td><%=product.getProductName()%></td>
						<td><%=product.getProductPrice()%></td>
						<td><span id="productamount<%=product.getId()%>"><%=entry.getValue()%></span>
						</td>
						<td><span id="producttotal<%=product.getId()%>"><%=total%></span></td>
						<td>
							<button
								onclick="addToCart(<%=product.getId()%>,'-',<%=product.getProductPrice()%>)">-</button>
							<button
								onclick="addToCart(<%=product.getId()%>,'+',<%=product.getProductPrice()%>)">+</button>

						</td>
					</tr>
					<%
						}
					%>
					<tr>
						<td></td>
						<td></td>
						<td></td>
						<td id="number"><%=cartOne.getAmount()%></td>
						<td id="totals"><%=totals%></td>
						<td><a href=""
							onclick="placeOrder(<%=store.getStoreId()%>,<%=totals%>)">确定下单</a></td>
					</tr>
					<%
						}
					%>
				</tbody>
			</table>

		</div>
		<hr />
		<%
			}
		%>
	</div>
</body>
</html>